clear all

use "${source}ACS_2008_2021.dta" ,clear 

	
g highincome_WB=0 

replace highincome_WB=1 if bpld==	30060	/*	Uruguay	*/
replace highincome_WB=1 if bpld==	70020	/*	New Zealand	*/
replace highincome_WB=1 if bpld==	50040	/*	Taiwan	*/
replace highincome_WB=1 if bpld==	42100	/*	France	*/
replace highincome_WB=1 if bpld==	40500	/*	Sweden	*/
replace highincome_WB=1 if bpld==	40100	/*	Finland	*/
replace highincome_WB=1 if bpld==	45213	/*	Czech Republic	*/
replace highincome_WB=1 if bpld==	41410	/*	Northern Ireland	*/
replace highincome_WB=1 if bpld==	45710	/*	Croatia	*/
replace highincome_WB=1 if bpld==	16010	/*	Bermuda	*/
replace highincome_WB=1 if bpld==	41300	/*	United Kingdom, ns	*/
replace highincome_WB=1 if bpld==	42500	/*	Netherlands	*/
replace highincome_WB=1 if bpld==	50200	/*	Korea	*/
replace highincome_WB=1 if bpld==	49900	/*	Europe, ns.	*/
replace highincome_WB=1 if bpld==	45300	/*	Germany	*/
replace highincome_WB=1 if bpld==	43610	/*	Azores	*/
replace highincome_WB=1 if bpld==	71047	/*	Northern Mariana Islands	*/
replace highincome_WB=1 if bpld==	26043	/*	Bahamas	*/
replace highincome_WB=1 if bpld==	46100	/*	Latvia	*/
replace highincome_WB=1 if bpld==	41400	/*	Ireland	*/
replace highincome_WB=1 if bpld==	50100	/*	Japan	*/
replace highincome_WB=1 if bpld==	26060	/*	Trinidad and Tobago	*/
replace highincome_WB=1 if bpld==	41000	/*	England	*/
replace highincome_WB=1 if bpld==	45500	/*	Poland	*/
replace highincome_WB=1 if bpld==	41100	/*	Scotland	*/
replace highincome_WB=1 if bpld==	30020	/*	Chile	*/
replace highincome_WB=1 if bpld==	26057	/*	St. Kitts-Nevis	*/
replace highincome_WB=1 if bpld==	46200	/*	Lithuania	*/
replace highincome_WB=1 if bpld==	30040	/*	Guyana/British Guiana	*/
replace highincome_WB=1 if bpld==	43600	/*	Portugal	*/
replace highincome_WB=1 if bpld==	29900	/*	Americas, ns	*/
replace highincome_WB=1 if bpld==	53400	/*	Israel/Palestine	*/
replace highincome_WB=1 if bpld==	54300	/*	United Arab Emirates	*/
replace highincome_WB=1 if bpld==	45600	/*	Romania	*/
replace highincome_WB=1 if bpld==	15000	/*	Canada	*/
replace highincome_WB=1 if bpld==	40400	/*	Norway	*/
replace highincome_WB=1 if bpld==	43300	/*	Greece	*/
replace highincome_WB=1 if bpld==	40000	/*	Denmark	*/
replace highincome_WB=1 if bpld==	45400	/*	Hungary	*/
replace highincome_WB=1 if bpld==	45212	/*	Slovakia	*/
replace highincome_WB=1 if bpld==	42000	/*	Belgium	*/
replace highincome_WB=1 if bpld==	26044	/*	Barbados	*/
replace highincome_WB=1 if bpld==	43400	/*	Italy	*/
replace highincome_WB=1 if bpld==	42600	/*	Switzerland	*/
replace highincome_WB=1 if bpld==	43800	/*	Spain	*/
replace highincome_WB=1 if bpld==	21070	/*	Panama	*/
replace highincome_WB=1 if bpld==	45000	/*	Austria	*/
replace highincome_WB=1 if bpld==	50010	/*	Hong Kong	*/
replace highincome_WB=1 if bpld==	40200	/*	Iceland	*/
replace highincome_WB=1 if bpld==	70010	/*	Australia	*/
replace highincome_WB=1 if bpld==	54000	/*	Saudi Arabia	*/
replace highincome_WB=1 if bpld==	26042	/*	Antigua-Barbuda	*/
replace highincome_WB=1 if bpld==	45200	/*	Czechoslovakia	*/
replace highincome_WB=1 if bpld==	53600	/*	Kuwait	*/
	* no high income country 

	drop if  highincome_WB==1

	* KEEP ONLY MIGRANTS FROM NON HIGH-INCOME COUNTRY ACOORDING TO WORLD BANK
	
* Focus on the entry cohort arrived 2007-08-09-10-11-12

	keep if inrange(yrimmig,2007,2012)

*select birth cohort so that age- migration is 18-40 

	gen agemig=yrimmig-birthyr 

	keep if  inrange(agemig,18,40)



	
	
** married before or after migration: timing of last mariage 

	gen yearsmarriedpostmig=yrmarr-yrimmig
	replace yearsmarriedpostmig=. if yrmarr==0|yrimmig==0
	gen sameyear_marriedmig=yearsmarriedpostmig==0 if yearsmarriedpostmig~=.

  *When did ever-married migrants LAST marry
	g       status_evermarried = 1 if  cond(yearsmarriedpostmig<0,1,0) &  yearsmarriedpostmig~=. 
	replace status_evermarried = 2 if   yearsmarriedpostmig==0 & yearsmarriedpostmig~=.
	replace status_evermarried = 3 if  cond(yearsmarriedpostmig>0,1,0) & yearsmarriedpostmig~=.
	label def evermar 1 "Married before migrating " 2 "Married same year as migrating " 3 "Married after migrating" , modify 
	label values status_evermarried  evermar 
	label var status_evermarried "When did ever-married migrants last marry"
	gen evermarried=marrno>=1 & marrno<=3
	
	gen married_before=  status_evermarried == 1 
	
	
	
   g married_beforemig = cond(yearsmarriedpostmig<0,1,0) if yearsmarriedpostmig~=. 
   g married_aftermig  = cond(yearsmarriedpostmig>=0,1,0) if yearsmarriedpostmig~=. 
   g married_strictaftermig  = cond(yearsmarriedpostmig>0,1,0) if yearsmarriedpostmig~=. 
	
  replace married_beforemig=0 if marst==6  /* includes people that divorced just before migration*/
  label var 	married_beforemig "married  before migrating"

	label var  sameyear_marriedmig "married same year as migrated"  
	label var  married_strictaftermig "married after migration (strictly)"

	
	
 * Change marital status after migration : from married to separated (at least once)
 
   g       change_married_to_unmarried = .
  replace  change_married_to_unmarried  = 0 if  marrno ==1 & married_beforemig==1 & inlist(marst,1,2)
  replace  change_married_to_unmarried  = 1 if  marrno ==1 & married_beforemig==1 & inlist(marst,3,4,5) /*assume that people get divorced after mig and not before*/
	
	*for people married twice of more 
    replace  change_married_to_unmarried  = 0 if inlist(marrno,2,3) &  married_beforemig==1 & inlist(marst,1,2)    /*last time married was before migration and still married*/  
    replace  change_married_to_unmarried  = 1 if inlist(marrno,2,3) &  married_beforemig==1 & inlist(marst,3,4,5)  /*last time married was before migration and now separated*/  

	*assumption to have a upper bound of change from married tu unmarried :
			*first marriage occured before mig and divorced after mig
    replace   change_married_to_unmarried = 1 if inlist(marrno,2,3) &   married_aftermig==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/  
	
	*assumption to have a lower bound of change from married tu unmarried :
			*first marriage occured after migration 
    g         change_married_to_unmarried_v2 =  change_married_to_unmarried  
	replace   change_married_to_unmarried_v2  =  . if inlist(marrno,2,3) &   married_aftermig==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/  
	
	g change_married_to_unmarried_v3= change_married_to_unmarried_v2 
	
 
 * Change marital status after migration : from unmarried to married (at least one)

	g        change_unmarried_to_married  = .
	replace  change_unmarried_to_married  = 0 if  marst==6 
	replace  change_unmarried_to_married  = 1 if  marrno ==1  & married_strictaftermig ==1  /*stricly after migration*/
	
	g       change_unmarried_to_married_v2 = change_unmarried_to_married
	replace change_unmarried_to_married_v2 = 1  if inlist(marrno,2,3) &   married_strictaftermig ==1 &  inlist(marst,1,2,3,4,5)  /*last time married was after migrationa and married twice*/   
	
	g change_unmarried_to_married_v3= change_unmarried_to_married 
	
	
	
	rename change_married_to_unmarried_v3  married_to_unmarried 
	rename change_unmarried_to_married_v3  unmarried_to_married  
	
	
	* ambigous marital status before migration : same year as mig or married twice and last marriage after mig 
	
	
	replace married_before = 9  if status_evermarried ==2
	replace married_before = 9 if status_evermarried ==3 & inlist(marrno,2,3)

	
	
		
save 	"${data}ACS_2008_2021_coh0712.dta" , replace






	
********************************************************************************************	
********************************************************************************************


 use  "${data}ACS_2008_2021_coh0712.dta" ,clear 
	
g t=1
collapse (sum) t perwt, by(yrimmig year married_before )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 

g cohort="2007-2012" 

collapse (sum) t perwt, by(cohort yrusa married_before  )
reshape wide t perwt, i(cohort yrusa) j(married_before )

rename t0 N_unmar 
rename perwt0 perwt_unmar 
rename t1  N_mar 
rename perwt1   perwt_mar 
rename  perwt9  perwt_mis

foreach v in mar unmar mis {
g ind_`v' = perwt_`v' if yrusa==1
egen inflow_`v'=max(ind_`v') 
g return_`v'= 100*(1-   perwt_`v'/inflow_`v')
}
gen perwt=  perwt_unmar + perwt_mar
gen return =  100*(1-   ( perwt_unmar + perwt_mar+perwt_mis)/(inflow_mar+inflow_unmar +inflow_mis) )
gen return_nomis =  100*(1-   ( perwt_unmar + perwt_mar)/(inflow_mar+inflow_unmar) )

 keep if yrusa<=9
 keep yrusa cohort perwt_unmar perwt_mar perwt_mis return_mar return_unmar return_mis return return_nomis inflow* 
 save temp,replace 
 
 
 **** only unmarried before 
 
 use 	"${data}ACS_2008_2021_coh0712.dta" ,clear 
	
	keep if married_before==0 
collapse (sum)  perwt, by(yrimmig year unmarried_to_married  )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 
g cohort="2007-2012" 
collapse (sum)  perwt, by(cohort yrusa unmarried_to_married  )
reshape wide perwt, i(cohort yrusa) j(unmarried_to_married  )
 keep if yrusa<=9
rename perwt0 perwt_stayunmar_aftmig  
rename perwt1 perwt_gotmar_aftmig   
  save temp2,replace 
  
  
 **** only married before 
 
 use 	"${data}ACS_2008_2021_coh0712.dta"  ,clear 
	
	keep if married_before==1 	
collapse (sum)  perwt, by(yrimmig year married_to_unmarried  )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 
g cohort="2007-2012" 
collapse (sum)  perwt, by(cohort yrusa  married_to_unmarried  )
reshape wide perwt, i(cohort yrusa) j( married_to_unmarried  )
 keep if yrusa<=9
rename perwt0 perwt_staymar_aftmig  
rename perwt1 perwt_gotunmar_aftmig   
	
  save temp3,replace 
  

 * ALL DATA 
  use temp,clear 
  merge 1:1 cohort yrusa  using temp2 
  drop _m
  merge 1:1 cohort yrusa  using temp3 
  drop _m
 
 g sex="all"
	save temp_all, replace 
 
 
************************************************************************ 
***** SAME BY GENDER ********
************************************************************************ 

foreach s in 1 2 {
	
use 	"${data}ACS_2008_2021_coh0712.dta" ,clear 
keep if sex==`s'	
g t=1
collapse (sum) t perwt, by(yrimmig year married_before )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 

g cohort="2007-2012" 

collapse (sum) t perwt, by(cohort yrusa married_before  )
reshape wide t perwt, i(cohort yrusa) j(married_before )

rename t0 N_unmar 
rename perwt0 perwt_unmar 
rename t1  N_mar 
rename perwt1   perwt_mar 
rename  perwt9  perwt_mis

foreach v in mar unmar mis {
g ind_`v' = perwt_`v' if yrusa==1
egen inflow_`v'=max(ind_`v') 
g return_`v'= 100*(1-   perwt_`v'/inflow_`v')
}
gen perwt=  perwt_unmar + perwt_mar
gen return =  100*(1-   ( perwt_unmar + perwt_mar+perwt_mis)/(inflow_mar+inflow_unmar +inflow_mis) )
gen return_nomis =  100*(1-   ( perwt_unmar + perwt_mar)/(inflow_mar+inflow_unmar) )

 keep if yrusa<=9
 keep yrusa cohort perwt_unmar perwt_mar perwt_mis return_mar return_unmar return_mis return return_nomis inflow* 
 save temp,replace 
 
 **** only unmarried before 
 use 	"${data}ACS_2008_2021_coh0712.dta"  ,clear 
	keep if sex==`s'
	keep if married_before==0 
collapse (sum)  perwt, by(yrimmig year unmarried_to_married  )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 
g cohort="2007-2012" 
collapse (sum)  perwt, by(cohort yrusa unmarried_to_married  )
reshape wide perwt, i(cohort yrusa) j(unmarried_to_married  )
 keep if yrusa<=9
rename perwt0 perwt_stayunmar_aftmig  
rename perwt1 perwt_gotmar_aftmig   
  save temp2,replace 
  
  
 **** only married before 
 
 use 	"${data}ACS_2008_2021_coh0712.dta"  ,clear 
	keep if sex==`s'
	keep if married_before==1 	
collapse (sum)  perwt, by(yrimmig year married_to_unmarried  )
drop if yrimmig ==year /*miss many migrants who arrive too late in the year in which the census is field*/
g yrusa =  year-yrimmig 
g cohort="2007-2012" 
collapse (sum)  perwt, by(cohort yrusa  married_to_unmarried  )
reshape wide perwt, i(cohort yrusa) j( married_to_unmarried  )
 keep if yrusa<=9
rename perwt0 perwt_staymar_aftmig  
rename perwt1 perwt_gotunmar_aftmig   
	
  save temp3,replace 
  

 * ALL DATA 
  use temp,clear 
  
  merge 1:1 cohort yrusa  using temp2 
  drop _m
  merge 1:1 cohort yrusa  using temp3 
  drop _m
  
  cap drop sex
  if `s'==1{
	gen sex="male"
  }
   
  if `s'==2{
	gen sex="female"
  }
  save temp_sexe`s', replace 
 
}



	use temp_all 
	append using temp_sexe1
	append using temp_sexe2
	
  erase temp_all.dta
  erase temp_sexe1.dta
  erase temp_sexe2.dta
  erase temp.dta 
  erase temp2.dta 
  erase temp3.dta 
  
  
  

  
  keep yrusa cohort perwt_unmar perwt_mar inflow_mar return_mar inflow_unmar return_unmar perwt_stayunmar_aftmig perwt_gotmar_aftmig perwt_staymar_aftmig perwt_gotunmar_aftmig sex
  order  yrusa sex cohort, first
  save "${data}ACS_collapsed_2008_2021.dta" ,replace 
  
 
 erase "${data}ACS_2008_2021_coh0712.dta" 
  
	
	
	
	
	